package com.yt.order.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.db.utils.RedisTokenUtil;
import com.yt.order.common.config.YmlConfig;
import com.yt.order.mapper.WorkOrderInfoMapper;
import com.yt.order.models.bo.WorkOrderInfoSaveBO;
import com.yt.order.models.dto.WorkOrderInfoDTO;
import com.yt.order.models.service.IWorkOrderInfoService;
import com.yt.order.models.vo.StatisticsVO;
import com.yt.order.models.vo.WorkOrderInfoVO;
import com.yt.product.models.dto.ProductInfoDTO;
import com.yt.product.models.service.IProductInfoService;
import com.yt.user.models.dto.CustomerInfoDTO;
import com.yt.user.models.service.ICustomerInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 系统权限信息控制器
 *
 * @author zhou
 * @Date 2025-09-06 10:41:50
 */
@DubboService
public class WorkOrderInfoServiceImpl extends ServiceImpl<WorkOrderInfoMapper, WorkOrderInfoDTO> implements IWorkOrderInfoService {

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private ICustomerInfoService customerInfoService;
    @DubboReference
    private IProductInfoService productInfoService;
    @Override
    public boolean add(WorkOrderInfoSaveBO saveBO) {
        WorkOrderInfoDTO dto = new WorkOrderInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public StatisticsVO queryStatis() {
        StatisticsVO statisticsVO = baseMapper.querySta();
        return statisticsVO;
    }

    @Override
    public boolean addOrder(WorkOrderInfoSaveBO bo) {
        WorkOrderInfoDTO dto = new WorkOrderInfoDTO();
        BeanUtils.copyProperties(bo, dto);
        if(bo.getCustomerId()!=null){
            dto.setType(0);
        }
        //设置主键
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        //价格
        BigDecimal price=BigDecimal.ZERO;
        //如果是企业级客户下单费用为10块钱，优先级为高的费用为8块钱，其他为5块钱
        //1.查询用户是企业级用户还是普通用户
        if(bo.getCustomerId()!=null) {
            CustomerInfoDTO customerInfoDTO = customerInfoService.selectById(bo.getCustomerId());
            if (customerInfoDTO != null && customerInfoDTO.getType()==1) {
                price=price.add(BigDecimal.valueOf(10));
            }
        }
        else {
            if (bo.getPriority() == 3) {
                price = price.add(BigDecimal.valueOf(8));
            } else {
                price = price.add(BigDecimal.valueOf(5));
            }
        }
        //2.计算产品价格
        if(bo.getProductId()!=null){
            ProductInfoDTO productInfoDTO = productInfoService.selectById(bo.getProductId());
            System.out.println(productInfoDTO);
            BigDecimal totalPrice = productInfoDTO.getPrice().multiply(BigDecimal.valueOf(bo.getProductQuantity()));
            price = price.add(totalPrice);
        }
        dto.setPrice(price);
        //如果接单员不为空，则状态为处理中
        if (bo.getEmployeeId() != null) {
            dto.setStatus(2);
        }
        return this.insert(dto);
    }

    @Override
    @Transactional
    public boolean deleteEmployeeOrder(Long id) {
        //往员工退单表插入数据
        baseMapper.insertRet(id);
        //清除员工，返回待接单状态
        boolean flag = baseMapper.deleteEmployee(id);

        return flag;
    }

    @Override
    public WorkOrderInfoVO getworkOrderInfoVOById(WorkOrderInfoSaveBO saveBO) {
        Wrapper<WorkOrderInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("id", saveBO.getId());
        WorkOrderInfoDTO workOrderInfoDTO = this.selectOne(wrapper);
        if (workOrderInfoDTO == null) {
            return null;
        }
        WorkOrderInfoVO vo = new WorkOrderInfoVO();
        BeanUtils.copyProperties(workOrderInfoDTO, vo);
        return vo;
    }

    @Override
    public Boolean updateById(WorkOrderInfoSaveBO saveBO) {
        WorkOrderInfoDTO dto = new WorkOrderInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        boolean falg = this.updateById(dto);
        return falg;
    }

    @Override
    public List<Long> queryCountId(String operator, String val) {
        Integer value = Integer.parseInt(val);
        List<Long> ids = baseMapper.queryEmployeeId(operator,value);
        return ids;
    }

    @Override
    public StatisticsVO queryAppSta(Long id) {
        Wrapper<CustomerInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("id", id);
        CustomerInfoDTO customerInfoDTO=customerInfoService.selectOne(wrapper);
        StatisticsVO statisticsVO =new StatisticsVO();
        if(customerInfoDTO==null){
            //查询员工的统计
            statisticsVO = baseMapper.queryEmpSta(id);
        }
        else {
            //查询客户统计
            statisticsVO =baseMapper.queryCusSta(id);
        }
        return statisticsVO;
    }

}
